var chargingStationData = [];
var chargingChart = undefined;
var outputPowerData = [];
var dateList = [];
var dataFlag = 1;
var outputMaxPower = 0;
var index = undefined;
var rate = 1;

$(document).ready(function() {
	showLoading();
	$('title', parent.document).html($('title').text());
	setBaseUrl();
	selectDate("startDate");
	selectDate("endDate");
	$ajaxList('chargingStation', getChargingStation, {BulidStatus: '3'});
	setTimeout(hideLoading, 500);
});
function setRate(){
	rate = $("#rate").val();
}
function getChargingStation(data) {
	var length = data.length;
	chargingStationData = data;
	for (var i = 0; i < length; i++) {
		$("#chargingStationID").append("<option value='" + data[i].chargingStationID + "'>" + data[i].chargingStationName + "</option>");
	}
}
function mores(){
	dataFlag = dataFlag + 1;
	if((dataFlag - 1) * 3 < dateList.length){
		searchFor();
	}else{
		$(".more").html('');
		$(".more").html('已加载全部');
	}
}
function search(){
	$("#curves").empty();
	dateList = [];
	var chargingStationID = $("#chargingStationID").val();
	var startChargingTime = $("#startChargingTime").val();
	if(!chargingStationID){
		$("#chargingStationID").focus();
		$("#chargingStationID").tips({
			side : 2,
			msg : "请选择充电站",
			bg : '#AE81FF',
			time : 1
		});
	} else if (!startChargingTime){
		$("#startChargingTime").focus();
		$("#startChargingTime").tips({
			side : 2,
			msg : "请选择开始时间",
			bg : '#AE81FF',
			time : 1
		});
	} else {
		var days = (new Date(date2String(new Date())) - new Date(startChargingTime))/(1000*3600*24) + 1;
		for(var i = 0; i < days; i++){
			var array = startChargingTime.split(/[^0-9]/ig);
			dateList.push(date2String(new Date(array[0], array[1] - 1, parseInt(array[2]) + i)));
		}
		index = 0;
		searchFor();
	}
}
function searchFor(){
	if(dateList[index] && index < dataFlag * 3){
		var div = $("#curves");
		$('<div id="'+index+'">').appendTo(div);
		$('<div class="col-sm-12" id="chart'+index+'" style="height:300px;">').appendTo(div);
		$("#" + index).html('<span>' + dateList[index] + '</span>');
		chargingChart = echarts.init(document.getElementById('chart' + index));
		chargingChart.showLoading({text : "功率曲线加载中..."});
		$ajax('realTimeCharging', {chargingStationID: $("#chargingStationID").val(), chargingType: "STATION", startChargingDTTM: dateList[index] + " 00:00:00", endChargingDTTM: dateList[index] + " 23:59:59"}, searchPowerChart, '', searchPowerChart);
	}
}
function searchPowerChart(data){
	if (data.outputMaxPower && data.outputMaxPower > 0) {
		outputMaxPower = data.outputMaxPower;
	}
	if(data && data.outputPower && data.outputPower.length > 0){
		outputPowerData = data.outputPower || [];
	}
	setTimeout(function (){chargingChart.hideLoading();}, 200);
	changeEchart();
	index = index + 1;
	setTimeout(searchFor, 200);
}
function changeEchart(){
	var option = {
			tooltip : {
				trigger: 'axis',
				axisPointer:{
            		label: {
            			formatter: function (params) {
            				if (params && params.value) {
            					return timeStamp2String(new Date(params.value)).substr(5);
            				} else {
            					'时间异常';
            				}
            			}
            		}
            	}
			},
			xAxis: [
				{
					type: 'time',
					min: dateList[index] + " 00:00:00",
					max: dateList[index] + " 23:59:59",
					splitNumber: outputPowerData && outputPowerData.length && outputPowerData.length < 10 ? outputPowerData && outputPowerData.length : 10,
					axisLabel: {
						formatter : function (chargingTime) {
							if(chargingTime){
                				return timeStamp2String(new Date(chargingTime)).substr(11);
                			} else {
                				return '';
                			}
						}
					}
				}
			],
			yAxis: [
				{
					type: 'value',
					min: 0,
					max: outputMaxPower/rate,
					position: 'left',
					axisLine: {
                        lineStyle: {
                            color: '#43d3ff',
                        }
                    },
					axisLabel: {
						formatter: '{value}'
					}
				}
			],
			series: [
				{
					name:'充电站功率',
					type:'line',
					symbol:'none',
					itemStyle: {
						normal : {
							color: '#43d3ff',
							areaStyle: {type: 'default'}
						}
					},
					data:outputPowerData
				}
			]
		};
	chargingChart.setOption(option);
}